#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
from _zsgk import zsgk_storage

zsgk_base_url = "https://api.eol.cn/gkcx/api/?access_token=&admissions=&central=&department=&dual_class=&" \
                "f211=&f985=&is_dual_class=&keyword=&page={0}&province_id=&request_type=1&school_type=&" \
                "signsafe=&size=20&sort=view_total&top_school_id=&type=&uri=apidata/api/gk/school/lists"
zsgk_page_count = 149
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0"
}
def universities_data():
    university_list = []
    urls = [zsgk_base_url.format(i) for i in range(1, zsgk_page_count)]
    for url in urls:
        print(url)
        r = requests.get(url, headers=headers)
        r.encoding = r.apparent_encoding
        universities = r.json().get("data").get("item")
        for university in universities:
            univer = {}
            univer["id"] = university.get("id")
            univer["school_name"] = university.get("name")
            univer["type"] = university.get("type")
            univer["level"] = university.get("level")
            univer["nature"] = university.get("nature")
            univer["belong"] = university.get("belong")
            univer["central"] = university.get("central")
            univer["department"] = university.get("department")
            univer["doublehigh"] = university.get("doublehigh")
            univer["f211"] = university.get("f211")
            univer["f985"] = university.get("f985")
            univer["is_recruitment"] = university.get("is_recruitment")
            univer["dual_class"] = university.get("dual_class_name")
            univer["address"] = university.get("address")
            univer["province_name"] = university.get("province_name")
            univer["single_province"] = university.get("single_province")
            univer["city_name"] = university.get("city_name")
            univer["county_name"] = university.get("county_name")
            univer["view_total_number"] = university.get("view_total_number")
            university_list.append(univer)
    print(university_list)
    return university_list

if __name__ == '__main__':
    zsgk_storage.storage_mysql(universities_data())